
package com.tools.cleanmaster.model.db;

import android.content.Context;

import com.tools.cleanmaster.R;
import com.tools.cleanmaster.common.thread.ThreadPool;
import com.tools.cleanmaster.manager.TrashManager;
import com.tools.cleanmaster.model.item.AppTrashItem;
import com.tools.cleanmaster.utils.StorageUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class TrashesPubApi {
    protected static final String TAG = "TrashesPubApi";

    private static final String[] APK_PATH_SKIPPED = {
            StorageUtils.getExternalStorageSubDirectory(".android_secure"),
            StorageUtils.getExternalStorageSubDirectory("OpenRecovery"),
            StorageUtils.getExternalStorageSubDirectory("TitaniumBackup")
            //CleanConstant.APP_PATH + "systembackup/"
    };
    private static HashSet<String> sIgnorePackages = new HashSet<String>();

    public static boolean skipApkFile(String path) {
        for (String p : APK_PATH_SKIPPED)
            if (path.startsWith(p))
                return true;
        return false;
    }

    @SuppressWarnings("static-access")
    public static String getAppTrashInfo(Context cxt, List<String> appTrashDir) {
        if (appTrashDir.size() <= 0) {
            return null;
        }
        StringBuilder msgStr = new StringBuilder();
        for (int i = 0, len = appTrashDir.size(); i < len; i++) {
            msgStr.append(cxt.getString(R.string.app_name)).append("\n");
            msgStr.append(appTrashDir.get(i).replace("//", "/")).append("\n");
            List<String> files = TrashesPubApi.getAppTrashFiles(appTrashDir.get(i));
            if (files != null && files.size() > 0) {
                msgStr.append(cxt.getString(R.string.app_name)).append("\n");
                for (String path : files) {
                    msgStr.append(path).append("\n");
                }
            }
        }
        return msgStr.toString();
    }

    private static List<String> getAppTrashFiles(String dir) {
        List<String> files = new ArrayList<String>();
        if (dir.length() > 0) {
            File dirf = new File(dir);
            if (dirf.exists() && dirf.isDirectory()) {
                File[] allfiles = dirf.listFiles();
                for (File f : allfiles) {
                    files.add(f.getAbsolutePath());
                }
            }
        }
        return files;
    }

    public static void addIgnorePackage(String pkgname) {
        sIgnorePackages.add(pkgname);
    }

    public static void removeIgnorePackage(String pkgname) {
        sIgnorePackages.remove(pkgname);
    }

    public static void onPackageRemoved(final Context cxt, final String pkgName) {
        if (!StorageUtils.externalStorageAvailable()) {
            return;
        }

        new Thread(new Runnable() {

            @Override
            public void run() {
                final List<AppTrashItem> trashList = TrashManager.getInstance().scanUninstalledAppTrash(pkgName);

                if (trashList != null && !trashList.isEmpty()) {

                    ThreadPool.runOnUi(new Runnable() {

                        @Override
                        public void run() {
                            /*Intent intent = new Intent(cxt, TrashAppInstallOrUninstallActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra(TrashAppInstallOrUninstallActivity.EXTRA_FROM_INSTALL, false);
                            intent.putExtra(TrashAppInstallOrUninstallActivity.EXTRA_APP_NAME, trashList.get(0).appName);
                            intent.putExtra(TrashAppInstallOrUninstallActivity.EXTRA_PKG_NAME, pkgName);
                            final int size = trashList.size();
                            ArrayList<String> pathList = new ArrayList<String>(size + 1);
                            ArrayList<Integer> suggestCleanList = new ArrayList<Integer>(size + 1);
                            for (int i = 0; i < size; i++) {
                                AppTrashItem item = trashList.get(i);
                                pathList.add(item.filePath);
                                suggestCleanList.add(item.cleanSuggest);
                            }
                            intent.putStringArrayListExtra(TrashAppInstallOrUninstallActivity.EXTRA_FILE_PATHS, pathList);
                            intent.putIntegerArrayListExtra(TrashAppInstallOrUninstallActivity.EXTRA_SUGGEST_CLEAN, suggestCleanList);
                            cxt.startActivity(intent);*/
                        }

                    });
                } else {

                }
            }

        }).start();
    }

    public static void onPackageInstalled(final Context cxt, final String pkgName) {

    }
}
